<?php
/**
 * Static content controller.
 *
 * This file will render views from views/group/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/group-controller.html
 */
class GroupsController extends AppController {

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 * @throws NotFoundException When the view file could not be found
 *	or MissingViewException in debug mode.
 */

	public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('add', 'view', 'edit', 'index');
    }

    public function index() {
    	$this->paginate = array(
    		'limit' => 5,
    		'order' => array(
    			'Group.id' => 'asc',
    			'Group.name' => 'desc')
    		);
		$data = $this->paginate('Group');
        $this->set('groups', $this->paginate());
    }

    public function view($id = null) {
    	$this->Group->id = $id;
        if (!$this->Group->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
    	$this->set('group', $this->Group->findById($id));
    }

	public function add() {
		if ($this->request->isPost()) {
			$this->Group->create();
			if ($this->Group->save($this->request->data)) {
				$this->Session->setFlash(_('Your group has been saved.'));
				return $this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(_('Unable to add your group.'));
		}
	}

	public function edit($id) {
		$this->Group->id = $id;
        if (!$this->Group->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }

        $this->set('id', $id);
        $data = $this->Group->find('list', array('fields'=>array('Group.name'), 'conditions'=>array('Group.id'=>$id)));
        $this->set('name', $data[$id]);

        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Group->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'view', $id));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        } else {
            $this->request->data = $this->Group->read(null, $id);
            unset($this->request->data);
        }
	}
}
